package leetcode018;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.cn/problems/4sum/submissions/
 * 18. 四数之和
 * 给你一个由 n 个整数组成的数组 nums ，和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] （若两个四元组元素一一对应，则认为两个四元组重复）：
 *
 * 0 <= a, b, c, d < n
 * a、b、c 和 d 互不相同
 * nums[a] + nums[b] + nums[c] + nums[d] == target
 * 你可以按 任意顺序 返回答案 。
 */
public class Study018 {

    public List<List<Integer>> fourSum(int[] nums, int target) {
        //排序
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                if ((long)nums[j]> 0
                        && (long)nums[i] + (long)nums[j] > target) {
                    break;
                }
                for (int k = j + 1; k < nums.length - 1; k++) {
                    if (k > j + 1 && nums[k] == nums[k - 1]) continue;
                    if ((long)nums[k] > 0
                            && (long)nums[i] + (long)nums[j] + (long)nums[k]> target) {
                        break;
                    }
                    for (int l = k + 1; l < nums.length; l++) {
                        if (l > k + 1 && nums[l] == nums[l - 1]) continue;
                        if ((long)nums[l] > 0
                                && (long)nums[i] + (long)nums[j] + (long)nums[k] + (long)nums[l]> target) {
                            break;
                        }

                        if ((long)nums[i] + (long)nums[j] + (long)nums[k] + (long)nums[l] == target) {
                            res.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
                        }
                    }
                }
            }
        }
        return res;
    }


    public List<List<Integer>> fourSum2(int[] nums, int target) {
        //排序
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            //nums有序，如果当前位置连续4个数字相加大于target则跳出，因为后面只会越来越大
            if ((long)nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
            //同理，最大的3个数加上nums[i]小于target则i+1,因为nums[i]太小了，需要继续换个大一点的数值
            if ((long)nums[i] + nums[nums.length - 1] + nums[nums.length - 1] + nums[nums.length - 1] < target) continue;

            for (int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                if ((long)nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
                if ((long)nums[i] + nums[j] + nums[nums.length - 1] + nums[nums.length - 1] < target) continue;
                int l = j + 1, r = nums.length - 1;
                while (l < r) {
                    long sum = (long) nums[i] + nums[j] + nums[l] + nums[r];
                    if (sum == target) {
                        res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
                        while (l < r && nums[r] == nums[r - 1]) {
                            r--;
                        }
                        r--;
                        while (l < r && nums[l] == nums[l + 1]) {
                            l++;
                        }
                        l++;
                    } else if (sum > target) {
                        while (l < r && nums[r] == nums[r - 1]) {
                            r--;
                        }
                        r--;
                    } else if (sum < target) {
                        while (l < r && nums[l] == nums[l + 1]) {
                            l++;
                        }
                        l++;
                    }
                }
            }
        }
        return res;
    }


    public static void main(String[] args) {
        Study018 study018 = new Study018();
        System.out.println(study018.fourSum2(new int[]{1, 0, -1, 0, -2, 2}, 0));
        System.out.println(study018.fourSum2(new int[]{2,2,2,2,2}, 8));
        System.out.println(study018.fourSum2(new int[]{1,-2,-5,-4,-3,3,3,5}, -11));
        System.out.println(study018.fourSum2(new int[]{1000000000,1000000000,1000000000,1000000000,-1000000000,-1000000000,-1000000000,-1000000000}, 0));
        System.out.println(study018.fourSum2(new int[]{1000000000,1000000000,1000000000,1000000000}, -294967296));
        System.out.println(study018.fourSum2(new int[]{-1000000000,-1000000000,1000000000,-1000000000,-1000000000}, 294967296));
    }

}
